import random
import unittest
import requests
from parameterized import parameterized
import app



def get_au_header(json):
    """
    功能：组装登陆成功后的认证请求头字典信息
    :param json: 登陆请求体
    :return: 登陆成功后的认证请求头字典信息
    """
    # 1. 登陆
    login_url = 'http://ihrm-test.itheima.net/api/sys/login'
    resp = requests.post(login_url, json=json)

    # 2. 提取响应内容的data字段
    # 3. 组装成认证请求头字段
    au_header = {'Authorization': 'Bearer ' + resp.json().get('data')}
    # print(au_header)

    return au_header


def get_rand_mobile(start='13'):
    # 以start开头，后面跟一位4~9之间的任意一位数字，后面是8位随机数字
    temp = (start +
            str(random.randrange(4, 10)) +
            ''.join(str(random.choice(range(10))) for _ in range(8)))
    return temp


data = [
    ("员工管理业务场景",
     {"mobile": "13800000002", "password": "123456"},
     {"username": "mikejiang",
      "mobile": "xxxxxx",
      "timeOfEntry": "2021-08-02",
      "formOfEmployment": 1,
      "workNumber": "001",
      "departmentName": "偷偷学习组",
      "departmentId": "1422400509205118976",
      "correctionTime": "2021-08-30T16:00:00.000Z"},
     "1075383135371350016",
     {"username": "yyds"},
     {
         "resp_code": 200,
         "success": True,
         "code": 10000,
         "msg": "操作成功！"
     }
     )]


class TestUser(unittest.TestCase):
    # 定义普通实例方法，不要test开头
    def deal_assert(self, expect_result, resp):
        """
        功能：处理断言
        :param expect_result: 预期结果
        :param resp: 实际的响应对象
        :return: None
        """
        # 4. 断言
        self.assertEqual(expect_result.get('resp_code'), resp.status_code)
        self.assertEqual(expect_result.get('success'), resp.json().get('success'))
        self.assertEqual(expect_result.get('code'), resp.json().get('code'))
        self.assertEqual(expect_result.get('msg'), resp.json().get('message'))

    @parameterized.expand(data)
    def test_user(self, case_name, user_data, emp_info, emp_id, user_modify_info, expect_result):
        print(f'【{case_name}】接口测试')
        # 提取认证请求头
        app.HEADERS = get_au_header(user_data)
        print('app.HEADERS = ', app.HEADERS)
        au_header = get_au_header(user_data)
        print('au_header = ', au_header)

        mobile = get_rand_mobile()
        print(mobile)
        emp_info['mobile'] = mobile # 替换mobile字段内容
        user_url = 'http://ihrm-test.itheima.net/api/sys/user'
        # 1. 发送添加员工的请求，需要请求体，认证头
        resp = requests.post(user_url, json=emp_info, headers=au_header)
        # 2. 获取响应体的内容
        print(resp.json())
        # 3. 断言
        self.deal_assert(expect_result, resp)

        # 1. 提取员工的id，如果添加成功，获取，如果失败，写一个固定
        result = resp.json()
        if result.get('data') is None:
            # 添加员工失败，写一个固定
            user_id = emp_id
        else:
            user_id = result.get('data').get('id')

        print('user_id = ', user_id)

        # 2. 组装一个查询员工的url
        url_user_by_id = user_url + '/' + user_id
        print('url_query = ', url_user_by_id)

        # 3. 发请求，获取内容
        resp = requests.get(url_user_by_id, headers=au_header)
        print('url_query = ', url_user_by_id)
        print(resp.json())
        # 4. 断言
        self.deal_assert(expect_result, resp)

        # 1. 组装url
        # 2. 发请求，请求体，认证头
        resp = requests.put(url_user_by_id, json=user_modify_info, headers=au_header)
        print(resp.json())
        # 3. 断言
        self.deal_assert(expect_result, resp)
        # 1. 组装url
        # 2. 发请求，认证头
        resp = requests.delete(url_user_by_id, headers=au_header)
        print(resp.json())
        # 3. 断言
        self.deal_assert(expect_result, resp)



